package com.jianganming.decorator;

//装饰器模式
public class DecoratorTest {
    public static void main(String[] args){
       // Decorator decorator = new JianBin(new Jidan(new HuoTui()));
        //decorator.decorator();
        System.out.println("--------------");
        Decorator decorator1 = new JianBin(new HuoTui(new Jidan(new Jidan())));
        double price = decorator1.decorator();
        System.out.println("总共花了"+price+"元");
    }
}

interface Decorator{
    double decorator();
}

class JianBin implements Decorator{
    private Decorator decorator;

    public JianBin(){

    }

    public JianBin(Decorator decorator){
        this.decorator=decorator;
    }

    @Override
    public double decorator() {
        double price = 3.0d;
        System.out.println("普通煎饼:"+price+"元");
        if(decorator !=null){
            if(decorator instanceof JianBin){
                throw new RuntimeException("不能再次添加煎饼");
            }
            double price1 = this.decorator.decorator();
            price+=price1;
        }
        return price;
    }
}

class Jidan implements Decorator{
    private Decorator decorator;

    public Jidan(){

    }

    public Jidan(Decorator decorator){
        this.decorator=decorator;
    }

    @Override
    public double decorator() {
        double price = 1.5d;
        System.out.println("添加鸡蛋:"+price+"元");
        if(decorator !=null){
            if(decorator instanceof JianBin){
                throw new RuntimeException("不能再次添加煎饼");
            }
            double price1 = this.decorator.decorator();
            price+=price1;
        }
        return price;
    }
}

class HuoTui implements Decorator{
    private Decorator decorator;

    public HuoTui(){

    }

    public HuoTui(Decorator decorator){
        this.decorator=decorator;
    }

    @Override
    public double decorator() {
        double price = 1.0d;
        System.out.println("添加火腿肠:"+price+"元");
        if(decorator !=null){
            if(decorator instanceof JianBin){
                throw new RuntimeException("不能再次添加煎饼");
            }
            double price1 = this.decorator.decorator();
            price+=price1;
        }
        return price;
    }
}
